package com.winz.cultureapp.activity;

import android.content.Intent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.entity.ArtDesBean;
import com.winz.cultureapp.http.OnResponeListener;
import com.winz.cultureapp.http.util.HttpUitls;
import com.winz.cultureapp.utils.JsonUtil;
import com.winz.cultureapp.utils.Logger;

import butterknife.BindView;
import butterknife.OnClick;

public class ArtDesOtherVideoActivity extends BaseAppCompatActivity {
    @BindView(R.id.tv_top_title)
    TextView mTv_top_title;
    @BindView(R.id.wv_content)
    WebView mWv_news;

    private String artId;

    @Override
    protected int contentView() {
        return R.layout.activity_art_des_othervideo;
    }

    @Override
    protected void initViews() {
        mWv_news.getSettings().setJavaScriptEnabled(true);//设置WebView支持JS
        mWv_news.getSettings().setDefaultTextEncodingName("utf-8");//设置解码时的字符集
        mWv_news.getSettings().setLoadWithOverviewMode(true);
        mWv_news.getSettings().setUseWideViewPort(true);
        mWv_news.getSettings().setSupportZoom(true);
        mWv_news.getSettings().setBuiltInZoomControls(true);
        mWv_news.getSettings().setDomStorageEnabled(true);
        mWv_news.getSettings().setBuiltInZoomControls(false);
        mWv_news.setWebViewClient(new WebViewClient() {
            /*
            此处能拦截超链接的url,即拦截href请求的内容.
            */
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                mWv_news.loadUrl(url);
//                if (url.startsWith("http")) {
//
////                    // 调用系统默认浏览器处理url
////                    view.stopLoading();
////                    view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
//                    return true;
//                }else {
//                    showMsg("该链接不是http");
//                }
                return true;
            }

        });
    }

    @Override
    protected void loadDatas(Intent intent) {
        if (intent != null) {
            if (intent.getExtras() != null) {
                artId = intent.getExtras().getString(Constants.KEY.ART.ITEM_DES_ID);
                getList();
            }
        }
    }

    private void getList() {
        showProgressDialog();
        HttpUitls.onGet("art_detail", artId, new OnResponeListener<ArtDesBean>() {
            @Override
            public void onSuccess(ArtDesBean bean) {
                Logger.d("art_detail==>>", JsonUtil.getJsonFromObj(bean));
                if (bean.code == 0) {
                    mTv_top_title.setText(bean.data.title);
                    mWv_news.loadUrl(bean.data.contentUrl);

                } else {
                    showMsg(bean.msg);
                }
                dismissProgressDialog();
            }

            @Override
            public void onFailed(String error) {
                dismissProgressDialog();
                Logger.d("art_detail==>>", error);

            }
        });
    }

    @OnClick(R.id.fl_back)
    public void onClick() {
        finish();
    }
}
